Ext.onReady(function() {
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		height:40,
		items : [ {
			text : '返回',
			xtype : 'button',
			listeners:{
				click: function(){
					location.href="sysMgr/sys_setting";
				}
			}
		},'-',{
			xtype:'button',
			text: '添加',
			listeners:{
				click: function(){
					location.href="sysMgr/add_attach_type";
				}
			}
		}, {
			text : '删除',
			xtype : 'button',
			listeners:{
				click: function(){
					var grid = Ext.getCmp("attach_type_grid");
					var selectionModel = grid.getSelectionModel();
					var models = selectionModel.getSelection();
					var ids = [];
					for(var i=0; i<models.length;i++){
						ids[i] = models[i].get("Id");
					}
					Ext.Ajax.request({
						url: '/attach_type/delete',
						type: 'POST',
						params:{
							ids: ids
						},
						callback:function(options,success, response){
							if(success){
								var result  = Ext.decode(response.responseText);
								if(result.success){
									Ext.getCmp("attach_type_grid").getStore().reload();
								}
							}
						}
					});
				}
			}
		},{
			text: '刷新',
			xtype: 'button',
			listeners:{
				click: function(){
					Ext.getCmp("attach_type_grid").getStore().reload();
				}
			}
		}]
	});
	var store = Ext.create('Ext.data.Store', {
	    fields:['Id', 'Name', {
	    	name: 'AttachmentGroup',
	    	type: 'object'
	    }, 'Desp'],
	    autoLoad:true,
	    proxy: {
	        type: 'ajax',
	        url: '/attach_type/all',
	        reader: {
	            type: 'json',
	            root: 'data.rows'
	        }
	    }
	});
	var grid = Ext.create('Ext.grid.Panel', {
		id: 'attach_type_grid',
		margin: '0 5 5 5',
		selType : 'checkboxmodel',
		store: store,
		columns : [ {
			text : '序号',
			xtype : 'rownumberer',
			width : 40,
			sortable : false
		}, {
			text : '名称',
			flex : 1,
			dataIndex : 'Name'
		}, {
			text: '类型', 
			sortable : false,
			flex : 1,
			dataIndex: 'AttachmentGroup', 
			renderer: function(v){
				return v.Name;
			}
		}, {
			text : '描述',
			dataIndex : 'Desp',
			flex:1
		},{
			text: '详细',
			renderer: function(v, meta, record){
				return '<a href="attach_type/detail/'+record.get("Id")+'">详细</a>';
			}
		}]
	});

	var panel = Ext.create("Ext.panel.Panel", {
		title: '附件类型管理',
		border : false,
		bodyBorder : false,
		layout : 'fit',
		dockedItems : [ toolbar ],
		items : [ grid ]
	});
	Ext.create('Ext.Viewport', {
		layout : 'fit',
		style : 'background:none;background-color:white;',
		items : [ panel ]
	});
});